package com.hengpeng.itfints.service.borrowInfo.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import com.hengpeng.common.exception.ServiceResult;
import com.hengpeng.common.persist.QueryResult;
import com.hengpeng.common.persist.query.DynamicQuery;
import com.hengpeng.common.persist.query.Projections;
import com.hengpeng.common.persist.query.Restrictions;
import com.hengpeng.common.persist.query.constants.QueryConstants.JoinType;
import com.hengpeng.common.persist.query.constants.QueryConstants.QueryType;
import com.hengpeng.common.util.DateTimeUtil;
import com.hengpeng.itfinbase.en.BorrowStatus;
import com.hengpeng.itfinbase.en.DurationType;
import com.hengpeng.itfinbase.en.InputStatus;
import com.hengpeng.itfinbase.persist.BorrowInfo;
import com.hengpeng.itfinbase.persist.InvestInfo;
import com.hengpeng.itfinbase.persist.RepaymentPlan;
import com.hengpeng.itfints.controller.borrowInfo.vo.BorrowInfoVo;
import com.hengpeng.itfints.service.ServiceFactory;
import com.hengpeng.itfints.service.borrowInfo.BorrowInfoService;

@Lazy
@Service
public class BorrowInfoServiceImpl implements BorrowInfoService
{
	@Autowired
	private ServiceFactory serviceFactory;

	@Override
	public ServiceResult<List<BorrowInfo>> queryList(BorrowStatus[] status, Long borrowUserId, Integer curPage,
			Integer pageSize,String zt,String qx,String sy,String gm,String area,String guran)
	{
		DynamicQuery query = new DynamicQuery(BorrowInfo.class, "b");
		query.eq("inputStatus", InputStatus.SUCCESS);//标的录入成功的
		if (status != null && status.length > 0)
		{
			query.inArray("b.status", status);
		}
//		if (borrowUserId != null)
//		{
//			query.eq("b.borrowUserId", borrowUserId);
//		}
//		if (!StringHelper.isEmpty(zt)) {//状态
//			query.eq("b.status", BorrowStatus.valueOf(zt));
//		}
//		if (!StringHelper.isEmpty(qx)) {//期限
//			if ("less3".equals(qx)) {
//				query.lt("b.borrowDuration", 3);
//			} else if ("3to6".equals(qx)) {
//				query.ge("b.borrowDuration", 3);
//				query.lt("b.borrowDuration", 6);
//			} else if ("6to12".equals(qx)) {
//				query.ge("b.borrowDuration", 6);
//				query.lt("b.borrowDuration", 12);
//			} else if ("morethan12".equals(qx)) {
//				query.ge("b.borrowDuration", 12);
//			}
//		}
//		if (!StringHelper.isEmpty(sy)) {//收益
//			if ("less10".equals(sy)) {
//				query.lt("b.borrowRate", 10);
//			} else if ("10to15".equals(sy)) {
//				query.ge("b.borrowRate", 10);
//				query.lt("b.borrowRate", 15);
//			} else if ("15to20".equals(sy)) {
//				query.ge("b.borrowRate", 15);
//				query.lt("b.borrowRate", 20);
//			} else if ("morethan20".equals(sy)) {
//				query.ge("b.borrowRate", 20);
//			}
//		}
//		if (!StringHelper.isEmpty(gm)) {//规模
//			if ("less200".equals(gm)) {
//				query.lt("b.borrowMoney", 200000000);
//			} else if ("200to500".equals(gm)) {
//				query.ge("b.borrowMoney", 200000000);
//				query.lt("b.borrowMoney", 500000000);
//			} else if ("500to800".equals(gm)) {
//				query.ge("b.borrowMoney", 500000000);
//				query.lt("b.borrowMoney", 800000000);
//			} else if ("morethan800".equals(gm)) {
//				query.ge("b.borrowMoney", 800000000);
//			}
//		}
//		if (!StringHelper.isEmpty(area)) {//地区
//			query.createAlias(UserInfo.class, "u");
//			query.eqProperty("b.borrowUserId", "u.id");
//			query.addProjection(Projections.property("b.*"));
//			query.eq("u.personArea", area);
//		}
//		if (!StringHelper.isEmpty(guran)) {//担保机构
//			query.eq("b.guaranteeUserId", guran);
//		}
		query.desc("case b.status when 'RAISE' then 5 " + "when 'END' then 4 " + "when 'LOANING' then 3 "
				+ "when 'REPAYMENTING' then 2 " + "when 'REPAYMENTED' then 1 else 0 end  ");
		query.desc("b.createTime");
		query.setPageIndex(curPage);
		query.setPageSize(pageSize);
		query.setIsSearchTotalCount(true);
		query.setQueryType(QueryType.JDBC);
		QueryResult<BorrowInfo> result = serviceFactory.getPersistService().findWithPage(BorrowInfo.class, query);

		Long count = result.getTotalCount();
		ServiceResult<List<BorrowInfo>> serviceResult = new ServiceResult<List<BorrowInfo>>();// 设置返回值
		serviceResult.setReturnValue(result.getResults());
		serviceResult.setReturnListSize(count.intValue());
		return serviceResult;
	}

	@Override
	public Long queryCount(BorrowStatus[] status)
	{
		DynamicQuery query = new DynamicQuery(BorrowInfo.class);
		if (status != null && status.length > 0)
		{
			query.inArray("status", status);
		}
		return serviceFactory.getPersistService().count(query);
	}

	@Override
	public Long queryInvestCount(Long borrowId)
	{
		DynamicQuery query = new DynamicQuery(InvestInfo.class);
		query.eq("borrowInfoId", borrowId);
		return serviceFactory.getPersistService().count(query);
	}

	@Override
	public BorrowInfo getBorrowInfo(Long id, BorrowStatus status)
	{
		DynamicQuery query = new DynamicQuery(BorrowInfo.class);
		query.eq("id", id);
		if (status != null)
		{
			query.eq("status", status);
		}
		return serviceFactory.getPersistService().getEntity(query, BorrowInfo.class);
	}

	@Override
	public List<BorrowInfoVo> queryMyList(BorrowStatus status, Long borrowUserId)
	{
		/**
		 * select a.borrowName,a.borrowMoney,a.borrowDuration,a.borrowRate,a.repaymentedTime,a.status,
		 * a.borrowMoney,a.raiseMoney, sum(case b.status when 'UNSETTLE' then b.repayMoney/100 else 0 end), --待还本息
		 * sum(case b.status when 'UNSETTLE' then 1 else 0 end), --未还期数 sum(case b.status when 'REPAYMENTED' then
		 * b.repayMoney/100 else 0 end) --还款总额 from borrowinfo a,repaymentplan b where a.id=b.borrowInfoId and
		 * a.status=? and a.borrowUserId=? group by
		 * a.borrowName,a.borrowMoney,a.borrowDuration,a.borrowRate,a.repaymentedTime,a.status,
		 * a.borrowMoney,a.raiseMoney;
		 */
		List<BorrowInfoVo> dataList = new ArrayList<BorrowInfoVo>();
		DynamicQuery query = new DynamicQuery(BorrowInfo.class, "a");
		query.createAlias(RepaymentPlan.class, JoinType.LEFTJOIN, "b");
		query.on(Restrictions.eqProperty("a.id", "b.borrowInfoId"));
		query.addProjection(Projections.property("a.borrowName"));// 0
		query.addProjection(Projections.property("a.borrowMoney"));// 1
		query.addProjection(Projections.property("a.borrowDuration"));// 2
		query.addProjection(Projections.property("a.borrowRate"));// 3
		query.addProjection(Projections.property("a.repaymentedTime"));// 4
		query.addProjection(Projections.property("a.status"));// 5
		query.addProjection(Projections.property("a.borrowMoney"));// 6
		query.addProjection(Projections.property("a.raiseMoney"));// 7
		query.addProjection(Projections.property("sum(case b.status when 'UNSETTLE' then b.repayMoney + b.repayAddMoney else 0 end)"));// 8待还本息
		query.addProjection(Projections.property("sum(case b.status when 'UNSETTLE' then 1 else 0 end)"));// 9未还期数
		query.addProjection(Projections.property("sum(case b.status when 'REPAYMENTED' then b.repayMoney + b.repayAddMoney else 0 end)"));// 10还款总额
		query.addProjection(Projections.property("a.durType"));// 11
		query.addProjection(Projections.property("a.id"));// 12
		if (status != null)
		{
			query.eq("a.status", status);
		}
		else
		{// 默认显示还款中标的
			query.eq("a.status", BorrowStatus.REPAYMENTING);
		}
		query.eq("a.borrowUserId", borrowUserId);
		query.addGourp("a.borrowName");
		query.addGourp("a.borrowMoney");
		query.addGourp("a.borrowDuration");
		query.addGourp("a.borrowRate");
		query.addGourp("a.repaymentedTime");
		query.addGourp("a.status");
		query.addGourp("a.borrowMoney");
		query.addGourp("a.raiseMoney");
		query.addGourp("a.durType");
		query.setQueryType(QueryType.JDBC);
//		System.out.println(query.getSqlResult().getSql());
		List l = serviceFactory.getPersistService().findObject(null, query);

		if (l != null && l.size() > 0)
		{
			for (int i = 0; i < l.size(); i++)
			{
				Object[] obj = (Object[]) l.get(i);
				BorrowInfoVo vo = new BorrowInfoVo();
				vo.setId(Long.parseLong(String.valueOf(obj[12])));
				vo.setBorrowName(String.valueOf(obj[0]));
				vo.setBorrowMoney(Long.parseLong(String.valueOf(obj[1])));
				vo.setBorrowDuration(Integer.parseInt(String.valueOf(obj[2])));
				vo.setBorrowRate(Double.parseDouble(String.valueOf(obj[3])));
				vo.setRepaymentedTime(DateTimeUtil.formatToDate(String.valueOf(obj[4]), DateTimeUtil.DATE_FORMAT_YMD));
				vo.setStatus(BorrowStatus.valueOf(String.valueOf(obj[5])));
				vo.setBorrowMoney(Long.parseLong(String.valueOf(obj[6])));
				vo.setRaiseMoney(Long.parseLong(String.valueOf(obj[7])));
				vo.setUnRepayMoney(Long.parseLong(String.valueOf(obj[8])));// 待还本息
				vo.setUnRepayNum(Integer.parseInt(String.valueOf(obj[9])));// 未还期数
				vo.setTotalRepayMoney(Long.parseLong(String.valueOf(obj[10])));// 还款总额
				vo.setDurType(DurationType.valueOf(String.valueOf(obj[11])));
				dataList.add(vo);
			}
		}
		return dataList;
	}
}
